package com.tx.studentManager.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tx.studentManager.config.SystemConfig;
import com.tx.studentManager.utils.DWZJsonUtil;
/**
 * 
 * @描述 系统配置
 * @since studentManager-admin 1.1
 * @version 1.0 2015-10-9 下午1:25:25
 * @author 此生唯晴
 */
@Controller
@RequestMapping("/system")
public class SystemController {
	@Autowired
	private SystemConfig systemConfig;

	@RequestMapping("/config")
	public ModelAndView config() {
		ModelAndView modelAndView = new ModelAndView("system/config");
		Map<String, Object> configMap = new HashMap<String, Object>();
		configMap.put("pageTitle", systemConfig.getPageTitle());
		configMap.put("queryPageSize", systemConfig.getQueryPageSize());
		modelAndView.addObject("configMap", configMap);
		return modelAndView;

	}

	/**
	 * 
	 * @描述 修改配置
	 * @param key
	 * @param value
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", produces = "text/html;charset=UTF-8")
	String modifyConfig(@RequestParam(required = true) String key, @RequestParam(required = true) String value) {
		try {
			switch (key) {
			case "pageTitle":
				systemConfig.setPageTitle(value);
				break;
			case "queryPageSize":
				systemConfig.setQueryPageSize(Integer.valueOf(value));
				break;
			default:
				return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "操作失败,无此配置！", null, null);
			}
		} catch (Exception e) {
			return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_FAILURE_CODE, "操作失败！", null, null);
		}
		return DWZJsonUtil.simpleJsonStr(DWZJsonUtil.RESULT_SUCCESS_CODE, "修改成功！", null, null);
	}
}
